package com.artisan.service.wx.impl;

import com.artisan.dao.AddressInfosMapper;
import com.artisan.dao.AddressMapper;
import com.artisan.dao.CustomerMessageMapper;
import com.artisan.medium.wx.service.WxAddressService;
import com.artisan.medium.wx.service.WxMessageService;
import com.artisan.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/6/18.
 */
@Service("wxAddressService")
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private AddressInfosMapper addressInfosMapper;

    @Override
    public Map<String,Object> getAddressByUid(int uid) {
        Map<String,Object>map=new HashMap<String,Object>();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria acriteria = addressExample.createCriteria();
        acriteria.andUIdEqualTo(uid);
        acriteria.andStateEqualTo(0);
        List<Address> addresss = addressMapper.selectByExample(addressExample);
        map.put("addresss",addresss);
        return map;
    }

    @Override
    public void delAddress(int id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        address.setState(1);
        addressMapper.updateByPrimaryKey(address);
    }

    @Override
    public Address getAddressById(int id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void editAddress(Address address) {
        Address add=addressMapper.selectByPrimaryKey(address.getId());
        add.setAname(address.getAname());
        add.setAphone(address.getAphone());
        add.setAddress(address.getAddress());
        addressMapper.updateByPrimaryKey(add);
    }

    @Override
    public int addAddress(Address address) {
        address.setState(0);
        address.setDefaultAddress(false);
        addressMapper.insertSelective(address);
        return address.getId();
    }

    @Override
    public String getAddIdByName(String name) {

        Map<String,Object>map=new HashMap<String,Object>();
        AddressInfosExample addressExample = new AddressInfosExample();
        AddressInfosExample.Criteria acriteria = addressExample.createCriteria();
        acriteria.andNameEqualTo(name);
        List<AddressInfos> addresss = addressInfosMapper.selectByExample(addressExample);
        if(addresss.size()!=0){
            return addresss.get(0).getId();
        }
        return null;
    }

    @Override
    public void defaultAddress(int id,int uid) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria acriteria = addressExample.createCriteria();
        acriteria.andUIdEqualTo(uid);
        acriteria.andDefaultAddressEqualTo(true);
        List<Address> defAddress= addressMapper.selectByExample(addressExample);
        if(defAddress.size()!=0){
            defAddress.get(0).setDefaultAddress(false);
            addressMapper.updateByPrimaryKey(defAddress.get(0));
        }
        Address address= addressMapper.selectByPrimaryKey(id);
        address.setDefaultAddress(true);
        addressMapper.updateByPrimaryKey(address);
    }

    @Override
    public Address getAddressDefault(int uid) {

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria acriteria = addressExample.createCriteria();
        acriteria.andUIdEqualTo(uid);
        acriteria.andStateEqualTo(0);
        acriteria.andDefaultAddressEqualTo(true);

        List<Address> addresss = addressMapper.selectByExample(addressExample);
        if(addresss.size()!=0){
            return addresss.get(0);
        }
        return null;
    }


}
